/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import com.oas.thit._this.flight.xsd.InfoSmsRequestBody;
import com.oas.thit._this.flight.xsd.InfoSmsResponseBody;
import com.oas.thit.xmlmessage.xsd.Header;
import java.util.List;
import javax.xml.bind.JAXBElement;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import org.apache.commons.lang.StringUtils;
import th.manager.UserManager;
import th.model.Pengguna;
import th.stripes.BaseActionBean;
import th.stripes.webservice.WebServiceUtil;

/**
 *
 * @author fikri
 */
@UrlBinding("/thcc")
public class THCCActionBean extends BaseActionBean{
    
    @Validate(required=true, label="No KT")
    private String noKt;
    @Validate(required=true, label="Tarikh Penerbangan")
    private String dateDepart;    
    private List senaraiPenumpang;
    
    private String noAkaun;
    
    private String noPengenalan;
    
    private static String SUCCESSFUL = "0000";    
    private static String FLIGHT_ENQ_MSG_ID = "TPHJFLSMWD01";   
    private static String HEADER_ID_FLIGHT_ENQUIRY = "THSHFSM";  
    
    private Pengguna pengguna;
    
    @Inject
    private WebServiceUtil webUtil;
    
    @Inject
    private UserManager userManager;
    
    
    @DontValidate
    public Resolution flightScheduleForm() {        
        return new JSP("thcc/flightScheduleForm.jsp");
    }
    
    @DontValidate
    public Resolution showFormUser() {
        return new JSP("thcc/user_form.jsp");
    }
    
    @DontValidate
    public Resolution searchUser() {
        if (StringUtils.isBlank(noPengenalan) || StringUtils.isBlank(noAkaun)) {
            addSimpleError("Sila isi semua maklumat.");
            return new JSP("thcc/user_form.jsp");
        }
        pengguna = userManager.findUser( noPengenalan, noAkaun );
        if (pengguna == null) {
            addSimpleError("Pengguna tidak dijumpai.");
        }
        return new JSP("thcc/user_form.jsp");
    }
    
    public Resolution flightScheduleDetail() {
        String responseCode = "";
        
        try { // Call Web Service Operation
            com.oas.thit._this.services.THISServices service = new com.oas.thit._this.services.THISServices();
            com.oas.thit._this.services.THISServicesPortType port = service.getTHISServicesHttpSoap11Endpoint();
            // TODO initialize WS operation arguments here
            com.oas.thit._this.flight.xsd.InfoSmsRequest req = new com.oas.thit._this.flight.xsd.InfoSmsRequest();
            
            com.oas.thit._this.flight.xsd.ObjectFactory obj = new com.oas.thit._this.flight.xsd.ObjectFactory();
            
            com.oas.thit.xmlmessage.xsd.ObjectFactory objHeader = new com.oas.thit.xmlmessage.xsd.ObjectFactory();
            Header reqHead = objHeader.createHeader();
            reqHead.setMsgId( objHeader.createHeaderMsgId(FLIGHT_ENQ_MSG_ID));  
            reqHead.setResponseCode( objHeader.createHeaderResponseCode(SUCCESSFUL) );            
            reqHead.setTxid(objHeader.createHeaderTxid( webUtil.getHeaderID(FLIGHT_ENQ_MSG_ID, HEADER_ID_FLIGHT_ENQUIRY) ));
            
            InfoSmsRequestBody reqBody = obj.createInfoSmsRequestBody();
            reqBody.setTHFlightNo( obj.createInfoSmsRequestBodyTHFlightDate(noKt));
            reqBody.setTHFlightDate( obj.createInfoSmsRequestBodyTHFlightDate(dateDepart));
            
            JAXBElement<InfoSmsRequestBody> jReqBody = obj.createInfoSmsRequestBody(reqBody);
            JAXBElement<Header> jReqHead = objHeader.createEventHeader(reqHead);
            req.setHeader(jReqHead);
            req.setBody(jReqBody);           
            
            // TODO process result here
            com.oas.thit._this.flight.xsd.InfoSmsResponse result = port.hajjflightinfoSMS(req);
            Header header = result.getHeader().getValue();
            responseCode = header.getResponseCode().getValue();
            
            if ( responseCode.equals(SUCCESSFUL)) {
                InfoSmsResponseBody resp = result.getBody().getValue();
                
            }
        } catch (Exception ex) {
        // TODO handle custom exceptions here
        }        
        return new JSP("thcc/flightScheduleForm.jsp");
    }

    public String getNoKt() {
        return noKt;
    }

    public void setNoKt(String noKt) {
        this.noKt = noKt;
    }

    public String getDateDepart() {
        return dateDepart;
    }

    public void setDateDepart(String dateDepart) {
        this.dateDepart = dateDepart;
    }

    public List getSenaraiPenumpang() {
        return senaraiPenumpang;
    }

    public void setSenaraiPenumpang(List senaraiPenumpang) {
        this.senaraiPenumpang = senaraiPenumpang;
    }

    public Pengguna getPengguna() {
        return pengguna;
    }

    public void setPengguna(Pengguna pengguna) {
        this.pengguna = pengguna;
    }

    public String getNoAkaun() {
        return noAkaun;
    }

    public void setNoAkaun(String noAkaun) {
        this.noAkaun = noAkaun;
    }

    public String getNoPengenalan() {
        return noPengenalan;
    }

    public void setNoPengenalan(String noPengenalan) {
        this.noPengenalan = noPengenalan;
    }
    
}
